#include "napi/native_api.h"
#include "src/BartModel.h"
#include <vector>
#include <sstream>
#include <hilog/log.h>

std::vector<std::string> split(const std::string& str, char ch) {
    std::istringstream iss(str);
    std::string item;
    std::vector<std::string> res;
    while (std::getline(iss, item, ch)) {
        res.push_back(item);
    }
    return res;
}

std::vector<std::string> genTextPairs(const std::vector<std::string>& labels) {
    std::vector<std::string> res;
    for (auto label : labels) {
        res.push_back("This example is about " + label + ".");
    }
    return res;
}

std::string merge(const std::vector<std::string>& labels) {
    std::string res = "";
    for (auto label : labels) {
        res += label + "+";
    }
    return res.substr(0,res.length() - 1);
}

static napi_value GenLabels(napi_env env, napi_callback_info info)
{
    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);
    
    const size_t MAX_SIZE = 256;
    char buf[MAX_SIZE];
    size_t size = 0;
    napi_get_value_string_utf8(env, args[0], buf, MAX_SIZE, &size);
    std::string text(buf, size);

    napi_get_value_string_utf8(env, args[1], buf, MAX_SIZE, &size);
    std::string labelSet(buf, size);
    
    auto labels = split(labelSet,'+');
    auto textPairs = genTextPairs(labels);
    std::vector<std::string> filteredLabels;
    bart::BartModel model("/data/storage/el2/base/haps/entry/files/bart.pt","/data/storage/el2/base/haps/entry/files/");
    int counter = 0;
    for (auto textPair : textPairs) {
        auto probs = model.getPossibility(text, textPair);
        if (probs[0][0].item<double>() > 0.6) { 
            filteredLabels.push_back(labels.at(counter));
        }
        counter++;
        OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "[CLipNative]", "predict label is %{public}s is %{public}f", textPair.c_str(), probs[0][0].item<double>());
    }
    
    auto res = merge(filteredLabels);
    
    napi_value result;
    napi_create_string_utf8(env, res.c_str(), res.length(), &result);
    return result;
}

static napi_value Add(napi_env env, napi_callback_info info)
{
    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

    napi_value sum;
    napi_create_double(env, value0 + value1, &sum);

    return sum;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    napi_property_descriptor desc[] = {
        { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "genLabels", nullptr, GenLabels, nullptr, nullptr, nullptr, napi_default, nullptr }
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
    napi_module_register(&demoModule);
}
